package manager.cms.service.exec;

import common.SnowflakeIdWorker;
import manager.cms.dao.CmsContentCatDao;
import manager.cms.dao.CmsContentDao;
import manager.cms.service.JdExecutor;
import manager.cms.vo.CmsContentCatVO;
import manager.cms.vo.CmsContentVO;
import manager.feign.ShopIndexFloorThirdService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "discoverGoodsExec")
public class DiscoverGoodsExecImpl implements JdExecutor {
    @Autowired
    private ShopIndexFloorThirdService shopIndexFloorThirdService;
    @Autowired
    private CmsContentCatDao cmsContentCatDao;
    @Autowired
    private CmsContentDao cmsContenDao;
    @Override
    public void execute(Map<String,Object> paramsMap) {
        try {
            JSONObject jsonObject = null;
            CmsContentCatVO cmsContentCatVO=new CmsContentCatVO();
            String  discoverGoods=shopIndexFloorThirdService.getDiscoverGoods();
            String todayMonetizeStr=discoverGoods.substring(15,discoverGoods.length()-1);
            jsonObject = new JSONObject(todayMonetizeStr);
            JSONArray discoverGoodsData=(JSONArray)jsonObject.get("data");
            String focusMonetizeCmsContentId=String.valueOf(SnowflakeIdWorker.nextId());
            cmsContentCatVO.setName("发现好物");
            cmsContentCatVO.setId(focusMonetizeCmsContentId);
            cmsContentCatDao.addCmsContentCat(cmsContentCatVO);
            List<CmsContentVO> contentVOList=new ArrayList<CmsContentVO>();
            for(int i=0;i<discoverGoodsData.length();i++){
                CmsContentVO cmsContentVO=new CmsContentVO();
                String id=String.valueOf(SnowflakeIdWorker.nextId());
                JSONObject item=(JSONObject)discoverGoodsData.get(i);
                cmsContentVO.setId(id);
                cmsContentVO.setPic(item.get("goodsPic").toString());
                cmsContentVO.setTitle(item.get("recommendTheme").toString());
                cmsContentVO.setCategoryId(focusMonetizeCmsContentId);
                contentVOList.add(cmsContentVO);
            }
            cmsContenDao.addCmsContentBatch(contentVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
